package it.com.Test.a2_Serializable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestOfThenApply {

    static ExecutorService executor = Executors.newFixedThreadPool(3);

    /*
    apply 线程运行结果传递，但不返回最终结果
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start....");
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
//             int i = 10 / 0;
            System.out.println("异步任务睡眠3s，模拟一号任务");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenApply(t -> {
            try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
            return "hello + " + t;
        });
        System.out.println("main运行结果：" + completableFuture.get());
        System.out.println("main....end....");
/**
 main....start....
 当前线程：12
 异步任务睡眠3s，模拟一号任务
 运行结果：5
 main运行结果：hello + 5
 main....end....
  */

    }
}
